package wbhproject.model;

import java.net.MalformedURLException;
import java.net.URL;
import com.google.gson.Gson;

/**
 * Model-class defines, what a MeasurePoint is
 * 
 * @author Thomas Aulich
 * 
 */
public class MeasurePoint {
	private long id;
	private ControllerUnit controllerUnit;
	private String symbolName;
	private String registerAddress;
	private String comment;
	private String format;
	private int decimalDigits;
	private URL readValUrl;

	public MeasurePoint(long pId, ControllerUnit pControllerUnit, String pSymbolName,
			String pRegisterAddress, String pComment, String pFormat,
			int pDecimalDigits) throws MalformedURLException {
		id = pId;
		controllerUnit = pControllerUnit;
		symbolName = pSymbolName;
		registerAddress = pRegisterAddress;
		comment = pComment;
		format = pFormat;
		decimalDigits = pDecimalDigits;

		// Calculate URL of this MeasurePoint
		// based on the other known attributes
		readValUrl = new URL(controllerUnit.getReadValUrl() + "?" + "PDP,,R"
				+ registerAddress + "," + format);
	}
	
	public long getId() {
		return id;
	}

	public int getDecimalDigits() {
		return decimalDigits;
	}

	public URL getReadValUrl() {
		return readValUrl;
	}

	public static double getDoubleValue(MeasurePoint pMeasurePoint, long pValue) {
		double decimalDigits = pMeasurePoint.getDecimalDigits();
		double factor = Math.pow(10, decimalDigits);
		double result = pValue / factor;
		return (result);
	}

	public String getSymbolName() {
		return symbolName;
	}

	public String getComment() {
		return comment;
	}

	public String getDescription() {
		return "Station: " + controllerUnit.getStationName() + "; Alias: "
				+ controllerUnit.getStationAlias() + ";Symbolname: "
				+ symbolName + "; Can be reached for 'readval' at: "
				+ getReadValUrl();
	}

	public ControllerUnit getControllerUnit() {
		return controllerUnit;
	}
	
	public String getJSON() {
		Gson gson = new Gson();
		String jsonString = gson.toJson(this);
		return jsonString;
	}
}
